﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class TgbusLogin : LoginImplBase
    {
        public TgbusLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = String.Format("http://pay.tgbus.com/api/userlogin.ashx?u={0}&p={1}&fn=LOGINSTATE&r=0&rand={2}",
                _username, _password, _rand.NextDouble());
            HttpResult resp = TransferMgr.doGetPure(loginurl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("LOGINSTATE(1)") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string serverlist_url = "http://pay.tgbus.com/api/game/8/servers.aspx";
            resp = TransferMgr.doGetPure(serverlist_url, ref cookies);
            string content = resp.getContent();
            // 首先查找游戏地址
            //<a href="javascript:;" onclick="goServer('/api/login.ashx?gameid=8&appid=23')" >双线2服-烽火赤壁</a>
            string gameString = String.Format("双线{0}服", _acc.ServerId);
            Regex re = new Regex(String.Format("<a.*onclick=\"goServer\\('([^\"']*?)'\\)\".*?>{0}.*</a>", gameString));
            Match m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }

            string redirecturl = m.Groups[1].Value;
            ///api/login.ashx?gameid=8&appid=24
            m = Regex.Match(redirecturl, "^.*appid=(\\d+).*$");
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            redirecturl = String.Format("http://pay.tgbus.com/api/game/8/gamelogin.ashx?appid={0}", m.Groups[1].Value);
            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }

            string location = resp.getExtraHeader("Location");
            location = "http://pay.tgbus.com/api/game/8/" + location;
            resp = TransferMgr.doGetPure(location, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }

            content = resp.getContent();
            m = Regex.Match(content, ".*?iframe.src.*?=.*?['\"]+(.*?)['\"]+.*");
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }
            location = m.Groups[1].Value;

            processStartGame(location, result, ref cookies);
            return result;
        }
    }
}
